package tk.deep.domain.biz.entity;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * Created by boot on 8/19/15.
 */
public enum BizLifeCycle {
    STATUS_NORMAL((byte) 0),
    STATUS_FREEZEN((byte) -1),
    STATUS_CLOSED((byte) 120),
    STATUS_DELETED(Byte.MAX_VALUE);

    private final static ThreadLocal<ResourceBundle> lang = new ThreadLocal<ResourceBundle>();
    private final static String BASENAME = "enum";


    private final String key;
    private final byte value;

    private BizLifeCycle[] blcInit;

    BizLifeCycle(byte v) {
        this.value = v;
        key = name().toLowerCase().replace("_", "");
        blcInit = values();
    }

    public static Map<Byte, String> getKV(Locale local, BizLifeCycle blcs) {
        Map<Byte, String> map = null;
        //new HashMap<Byte, String>();
        if (local == null)
            local = Locale.getDefault();
        lang.set(ResourceBundle.getBundle(BASENAME, local));
        for (BizLifeCycle blc : blcs.values()) {
            map.put(blc.value, blc.getDesc());
        }
        return map;
    }

    private String getDesc() {
        return key == null || lang.get() == null || lang.get().containsKey(key) ? "" : lang.get().getString(key);

    }


    public String desc() {
        return text(key);
    }

    public String desc(Object[] o) {
        return text(key, o);
    }

    @Override
    public String toString() {
        return desc();
    }

    private static String text(String key) {
        return text(key, "");
    }

    private static String text(String key, Object[] o) {
        return new MessageFormat(text(key)).format(o);
    }

    private static String text(String key, String defaultDesc) {
        return key == null || lang.get() == null || !lang.get().containsKey(key) ? defaultDesc : lang.get().getString(key);
    }

    /*private static String text(String key, String defaultValue, Object[] o) {

        return new MessageFormat(text(key, defaultValue)).format(o);

    }*/

    /**
     * Set local with Local object, if null using the system default
     * locale setting.
     *
     * @param local
     */
    public static void setLocal(Locale local) {
        //ResourceBundle rb = ResourceBundle.getBundle("",Locale.getDefault());
        lang.set(ResourceBundle.getBundle(BASENAME, local));
    }


    public static void main(String[] args) {

        StringBuilder sbo = new StringBuilder();
        StringBuilder sbv = new StringBuilder();
        for (BizLifeCycle blc : BizLifeCycle.values()) {
            sbo.append(String.format("%n%s", blc.ordinal()));
            sbv.append(String.format("%n%s", blc.value));

            System.out.println(String.format("%n%-6s:%-6s|%-6s", blc.ordinal(), blc.value, blc.getDesc() + "fkdkd"));
        }

        System.out.println(String.format("%n######wo cao######%s############", Byte.MAX_VALUE));

        System.out.println(sbo.toString());
        System.out.println("===============");
        System.out.println(sbv.toString());


        System.out.println(String.format("%n============K \t:\tV==========="));


    }


    private static void testLocal(Locale loc) {
        if (loc == null)
            loc = Locale.getDefault();
        setLocal(loc);
        System.err.println("国际化资源完整性检查:" + BASENAME + "_" + loc + ".properties");

        for (BizLifeCycle blc : BizLifeCycle.values()) {
            if (blc.desc().equals("")) {
                System.err.println(blc.key + "  : Ha ha tested desc is null.");
            } else {

                System.out.println(blc.key + "=" + blc.desc());
            }
        }
    }
}
